/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import org.opu.db_vdumper.actions.db.DbAction;
import org.opu.db_vdumper.beans.ApplicationData;
import org.opu.db_vdumper.beans.UserData;
import org.opu.db_vdumper.io.FileIO;
import org.opu.db_vdumper.resources.ConfigPath;
import org.opu.db_vdumper.ui.component.db.ConnectionDialog;
import org.opu.db_vdumper.util.Logger;

/**
 *
 * @author yura
 */
public class DbConnect  extends AbstractAction{
    /** My name */
    public static final String MNAME = "DbConnect";
    private ApplicationData data;
    private final Map<String, DbAction> connectActions;

    public DbConnect(ApplicationData data, Map<String, DbAction> cActions) {
        this.data = data;
        this.connectActions = cActions;
    }

    @Override
    public String getName() {
        return MNAME;
    }

    @Override
    public void action(ActionEvent e) {
        OkAction ok = new OkAction(data, connectActions);
        Logger.getInstance().debug(this, "Create \"OkAction\" (" + ok + ")");
        CancelAction cancel = new CancelAction();
        Logger.getInstance().debug(this, "Create \"CancelAction\" (" + cancel + ")");

        UserData ud = null;
        try {
            Properties prop = new Properties();
            File file = ConfigPath.getInstance().getUserDbProp();
            FileIO.load(file, prop);
            ud = new UserData(prop);
            Logger.getInstance().info(this, "Data loaded from " + file);
        } catch (IOException ex) {
        }
        ConnectionDialog dialog = new ConnectionDialog(ud, cancel, ok);
        Logger.getInstance().debug(this, "Create \"dialog\" (" + dialog + ")");

        ok.setDialog(dialog);
        cancel.setDialog(dialog);

        dialog.addApproveAction(ok);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        Logger.getInstance().debug(this, "Show dialog");
    }

    class CancelAction extends OkAction {

        public CancelAction() {
            super(null, null);
        }

        @Override
        public void action(ActionEvent e) {
            close();
        }

        @Override
        public String getName() {
            return "Cancel";
        }
    }

}
